package com.xinghai.system.service.impl;

import java.util.List;
import com.xinghai.common.utils.DateUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Service;
import com.xinghai.system.mapper.JsGoodMapper;
import com.xinghai.system.domain.JsGood;
import com.xinghai.system.service.IJsGoodService;

/**
 * 商品Service业务层处理
 * 
 * @author xinghai
 * @date 2024-08-31
 */
@Service
public class JsGoodServiceImpl implements IJsGoodService 
{
    @Autowired
    private JsGoodMapper jsGoodMapper;

    @Autowired
    private SqlSessionFactory sqlSessionFactory;
    /**
     * 查询商品
     * 
     * @param id 商品主键
     * @return 商品
     */
    @Override
    public JsGood getInfo(Long id)
    {
        return jsGoodMapper.getInfo(id);
    }

    /**
     * 查询商品列表
     * 
     * @param jsGood 商品
     * @return 商品
     */
    @Override
    public List<JsGood> list(JsGood jsGood)
    {
            jsGood.setDelFlag("0");
        return jsGoodMapper.list(jsGood);
    }

    /**
     * 新增商品
     * 
     * @param jsGood 商品
     * @return 结果
     */
    @Override
    public int insert(JsGood jsGood)
    {
        jsGood.setCreateTime(DateUtils.getNowDate());
        return jsGoodMapper.insert(jsGood);
    }
    /**
     * 批量新增商品
     *
     * @param jsGoodList 商品
     * @return 结果
     */
    @Override
    public int insertBatch(List<JsGood> jsGoodList)
    {
        return jsGoodMapper.insertBatch(jsGoodList);
    }
    /**
     * 修改商品
     * 
     * @param jsGood 商品
     * @return 结果
     */
    @Override
    public int update(JsGood jsGood)
    {
        jsGood.setUpdateTime(DateUtils.getNowDate());
        return jsGoodMapper.update(jsGood);
    }

    /**
     * 批量删除商品
     * 
     * @param ids 需要删除的商品主键
     * @return 结果
     */
    @Override
    public int deleteBatch(Long[] ids)
    {
        return jsGoodMapper.deleteBatch(ids);
    }

    /**
     * 删除商品信息
     * 
     * @param id 商品主键
     * @return 结果
     */
    @Override
    public int delete(Long id)
    {
        return jsGoodMapper.delete(id);
    }

    /**
     * 导入商品信息
     *
     * @param list 商品信息集合
     * @return 结果
     */
    @Override
    @Transactional
    public boolean importData(List<JsGood> list){
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        try {
            JsGoodMapper jsGoodMapper = sqlSession.getMapper(JsGoodMapper.class);
            list.stream().forEach(item -> jsGoodMapper.insert(item));
            // 提交数据
            sqlSession.commit();
            sqlSession.rollback();
        } catch (Exception e) {
            sqlSession.rollback();
            return false;
        } finally {
            sqlSession.close();
        }
        return true;
    }
}
